package com.itheima.dubbo.api.mongo.impl;

import com.itheima.dubbo.api.mongo.CommentApi;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * 根据动态的id查询评论列表
     * @param page
     * @param pagesize
     * @param movementId
     * @return
     */
    @Override
    public PageResult findComments(Integer page, Integer pagesize, String movementId) {
        Query query = new Query(Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType").is(2));
        long count = mongoTemplate.count(query, Comment.class);
        query.skip((page-1)*pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        return new PageResult(page.longValue(),pagesize.longValue(),count,commentList);
    }

    /**
     * 保存点赞、评论、喜欢，返回点赞数、评论数、喜欢数
     * @param comment
     * @return
     */
    @Override
    public Integer save(Comment comment) {
         //1. 查询movement，给评论补全publishUserId
        Query query = new Query(Criteria.where("id").is(comment.getPublishId()));
        Movement movement = mongoTemplate.findOne(query, Movement.class);
        comment.setPublishUserId(movement.getUserId());
        //2. 插入评论
        mongoTemplate.save(comment);

        //3. 操作movement,人具体的评论数|点赞数|喜欢数 +1
        Update update = new Update();
        if(comment.getCommentType()==1) {
            update.inc("likeCount",1);
        }else if(comment.getCommentType()==2){
            update.inc("commentCount",1);
        }else{
            update.inc("loveCount",1);
        }

        //4. 设置更新的条件
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);

        Movement movementResult = mongoTemplate.findAndModify(query, update, options, Movement.class);
        return movementResult.statisCount(comment.getCommentType());
    }

    @Override
    public Integer delete(Comment comment) {
        //1. 根据条件去删除评论的数据
        Query query = new Query(Criteria.where("publishId").is(comment.getPublishId()).and("contentType").is(comment.getCommentType()).and("userId").is(comment.getUserId()));
        mongoTemplate.remove(query,Comment.class);

        //2. 操作movement,人具体的评论数|点赞数|喜欢数 +1
        Update update = new Update();
        if(comment.getCommentType()==1) {
            update.inc("likeCount",-1);
        }else if(comment.getCommentType()==2){
            update.inc("commentCount",-1);
        }else{
            update.inc("loveCount",-1);
        }

        //3.设置更新的条件
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);

        Query movementQuery  = new Query(Criteria.where("id").is(comment.getPublishId()));
        Movement movementResult = mongoTemplate.findAndModify(movementQuery, update, options, Movement.class);
        return movementResult.statisCount(comment.getCommentType());

    }
}